Skip to content

simple_renderer: Destroy resources when released, after the GPU is done - #139

Merged
natetrost merged 1 commit into
android:mainfrom
natetrost:main
Nov 24, 2026
Merged

natetrost merged 1 commit into
android:mainfrom
natetrost:main

Conversation

@natetrost

Copy link
Copy Markdown
Contributor

Resources were kept alive by the renderer until Destroy* was called, and then deleted at the start of the next frame:

  • Vulkan deleted them before waiting for any frame fence, while frames still in flight could be using them. Releasing a vertex buffer and texture right after drawing with them (a temporary test hook) gave VUID-vkDestroyBuffer-buffer-00922 and VUID-vkDestroySampler-sampler-01082 every frame.
  • A resource whose owner just dropped its pointer without Destroy* (e.g. PlayScene's fallback wall texture) stayed alive until shutdown.

Resources are now owned by their shared pointers: the pointers returned by Create* have a deleter that queues the resource for deferred deletion (RendererResources), and the renderer keeps no reference:

  • Vulkan: a queued resource is deleted once every frame slot's fence has been waited after its release (tracked when BeginFrame acquires an image), so no frame that could use it is still executing. The frame slots are the swapchain's frames in flight. Everything queued is deleted when the swapchain is recreated (after waiting for the device to be idle) and at shutdown.
  • GLES: at the next frame with the context current, as before.
  • A resource that releases others when deleted (e.g. a render state's shader program) queues them for a later deletion.
  • Live render passes are tracked weakly for the swapchain recreation framebuffer purge.
  • At shutdown, everything queued is deleted and resources still referenced are logged. From then on (also for a release on another thread racing the shutdown), a released resource can't be deleted (its device or context is gone) and is leaked with an error, instead of crashing.

GLES shutdown: the window (and surface) is usually gone by then (APP_CMD_TERM_WINDOW comes before APP_CMD_DESTROY), and the deletes ran without a current context ("call to OpenGL ES API with no current context"), leaking the objects. The renderer now makes its context current for the shutdown, without a surface if needed (EGL_KHR_surfaceless_context; GraphicsAPIResourcesGLES provides the display for it). A GLES resource only deletes its GL objects with the context it was created with current, so it never deletes another context's objects of the same name; a destroyed context took them with it.

Vulkan frame tracking: GetSwapchainFrameResourcesVk acquiring the image of a frame that was already acquired (and not presented) returned without waiting for the frame fence, so a second BeginFrame would reuse (and count as done) a frame still executing; it now fails. Frame resources report the swapchain's frame count, which RendererVk uses instead of assuming the display buffer mode didn't change.

Destroy* are now optional virtual no-ops in the Renderer base class, kept for compatibility. The README and interface documentation describe the new lifetime rules (and that UniformBuffer copies its element array).

agdktunnel:

  • TextureManager created another copy of every texture each time the loader scene ran (on every return to the main menu): 8 more live textures per play session. A texture that is already loaded is kept, and the loader scene doesn't read its file again.
  • TextureManager copies texture names and checks for a null name. TextureManager and GfxManager release their resources without Destroy* (which needed the renderer instance).

Resources were kept alive by the renderer until Destroy* was called, and
then deleted at the start of the next frame:
- Vulkan deleted them before waiting for any frame fence, while frames
  still in flight could be using them. Releasing a vertex buffer and
  texture right after drawing with them (a temporary test hook) gave
  VUID-vkDestroyBuffer-buffer-00922 and VUID-vkDestroySampler-sampler-01082
  every frame.
- A resource whose owner just dropped its pointer without Destroy* (e.g.
  PlayScene's fallback wall texture) stayed alive until shutdown.

Resources are now owned by their shared pointers: the pointers returned
by Create* have a deleter that queues the resource for deferred
deletion (RendererResources), and the renderer keeps no reference:
- Vulkan: a queued resource is deleted once every frame slot's fence has
  been waited after its release (tracked when BeginFrame acquires an
  image), so no frame that could use it is still executing. The frame
  slots are the swapchain's frames in flight. Everything queued is
  deleted when the swapchain is recreated (after waiting for the device
  to be idle) and at shutdown.
- GLES: at the next frame with the context current, as before.
- A resource that releases others when deleted (e.g. a render state's
  shader program) queues them for a later deletion.
- Live render passes are tracked weakly for the swapchain recreation
  framebuffer purge.
- At shutdown, everything queued is deleted and resources still
  referenced are logged. From then on (also for a release on another
  thread racing the shutdown), a released resource can't be deleted
  (its device or context is gone) and is leaked with an error, instead
  of crashing.

GLES shutdown: the window (and surface) is usually gone by then
(APP_CMD_TERM_WINDOW comes before APP_CMD_DESTROY), and the deletes ran
without a current context ("call to OpenGL ES API with no current
context"), leaking the objects. The renderer now makes its context
current for the shutdown, without a surface if needed
(EGL_KHR_surfaceless_context; GraphicsAPIResourcesGLES provides the
display for it). A GLES resource only deletes its GL objects with the
context it was created with current, so it never deletes another
context's objects of the same name; a destroyed context took them with
it.

Vulkan frame tracking: GetSwapchainFrameResourcesVk acquiring the image
of a frame that was already acquired (and not presented) returned
without waiting for the frame fence, so a second BeginFrame would reuse
(and count as done) a frame still executing; it now fails. Frame
resources report the swapchain's frame count, which RendererVk uses
instead of assuming the display buffer mode didn't change.

Destroy* are now optional virtual no-ops in the Renderer base class,
kept for compatibility. The README and interface documentation describe
the new lifetime rules (and that UniformBuffer copies its element
array).

agdktunnel:
- TextureManager created another copy of every texture each time the
  loader scene ran (on every return to the main menu): 8 more live
  textures per play session. A texture that is already loaded is kept,
  and the loader scene doesn't read its file again.
- TextureManager copies texture names and checks for a null name.
  TextureManager and GfxManager release their resources without
  Destroy* (which needed the renderer instance).

Tested on a Pixel 10 Pro with Vulkan Validation Layers 1.4.363.1 and a
temporary hook counting live Vulkan textures and buffers, over three
play/quit cycles, 6 s of creating and releasing a vertex buffer and
texture every frame during play, and quitting the app (renderer
shutdown):
- Before: the in-use VUIDs above while churning; textures 8 -> 40.
- After: no new validation messages (only the known Swappy semaphore
  VUID-vkQueueSubmit-pSignalSemaphores-00067), textures stay at 8 and
  buffers return to their count after each scene, nothing still
  referenced or leaked at shutdown.
- GLES (forced): the same sequence runs without errors, and the
  shutdown no longer calls GL without a current context.
- A host test (ASan/UBSan) covers the deletion timing per frame slot
  (including a frame count change), copies keeping a resource alive,
  resources released while deleting, the render pass registry, and
  release after shutdown and after the renderer is gone.
- Debug and release build.
@natetrost
natetrost merged commit a53f8c2 into android:main Nov 24, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant